import 'package:auto_route/auto_route.dart';
import 'package:casdon/app/config/env/common_config.dart';
import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/common/widget/common_app_bar.dart';
import 'package:casdon/common/widget/gradient_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';


@RoutePage()
class ServiceSubmitFailureScreen extends StatelessWidget {
  final String message;
  const ServiceSubmitFailureScreen({super.key, this.message = '网络故障，请检查网络后重试'});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar(title: ''),
      backgroundColor: Colors.white,
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: 48.h),

            CircleAvatar(
              radius: 22.5.r,
              backgroundColor: const Color(0xFFF5222D),
              child: Icon(Icons.close, color: Colors.white, size: 22.5.r),
            ),
            SizedBox(height: 16.h),
            Text('预约失败', style: context.textTheme.titleMedium),
            SizedBox(height: 8.h),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  width: double.infinity,
                  height: 50.h,
                  color: context.colorScheme.surface,
                  alignment: Alignment.center,
                  child: GestureDetector(
                    onTap: () => _callHotline(context),
                    behavior: HitTestBehavior.opaque,
                    child: RichText(
                      textAlign: TextAlign.center,
                      text: TextSpan(
                        style: context.textTheme.bodyMedium?.copyWith(
                          color: context.customColorThemeNonNull.textColor2,
                        ),
                        children: [
                          TextSpan(
                            text: '客服热线：',
                            style: context.textTheme.bodyMedium?.copyWith(
                              color: context.customColorThemeNonNull.textColor2,
                            ),
                          ),
                          TextSpan(
                            text: '400-811-0168',
                            style: context.textTheme.bodyMedium?.copyWith(
                              color: context.colorScheme.primary,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
            const Spacer(),
            Row(
              children: [
                Expanded(
                  child: OutlinedButton(
                    onPressed: () =>
                        Navigator.of(context).pop('main'), // 放弃预约，回到“我的”
                    style: OutlinedButton.styleFrom(
                      foregroundColor: context.colorScheme.primary,
                      minimumSize: Size(double.infinity, 44.h),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(22.r),
                      ),
                      side: BorderSide(color: context.colorScheme.primary),
                    ),
                    child: const Text('放弃预约'),
                  ),
                ),
                SizedBox(width: 12.w),
                Expanded(
                  child: GradientButton(
                    text: '重新预约',
                    height: 44.h,
                    borderRadius: 24,
                    onPressed: () => Navigator.of(context).pop(),
                  ),
                ),
              ],
            ),
            SizedBox(height: 166.h),
          ],
        ),
      ),
    );
  }

  Future<void> _callHotline(BuildContext context) async {
    final uri = Uri(scheme: 'tel', path: CommonConfig.servie_phone);
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri);
    } else if (context.mounted) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('无法拨打电话，请检查设备设置')));
    }
  }
}
